using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Diagnostics;
using System.Web;
using System.Web.UI;
using System.Xml;
using Microsoft.SharePoint;

namespace ARF.Web.Fields
{
    public class ARFProperties : NoteField
    {
        private XmlDocument _properties;
        private SPControlMode _previous;

        public ARFProperties()
        {
            FieldName = "__ARFProperties";
            
        }

        public bool IsEditingPostback
        {
            get
            {
                return (_previous == SPControlMode.Edit);
            }
        }

        protected void EnsureProperties()
        {
            if (_properties == null)
            {
                _properties = new XmlDocument();
                if (ViewState["ARFProperties"] != null)
                    _properties.LoadXml((string)ViewState["ARFProperties"]);
                else if (string.IsNullOrEmpty((string)ItemFieldValue))
                    _properties.LoadXml("<Properties/>");
                else
                    _properties.LoadXml((string)ItemFieldValue);
            }
        }

        public override object Value
        {
            get
            {
                EnsureProperties();

                return _properties.OuterXml;
            }
            set
            {
                EnsureProperties();

                if (string.IsNullOrEmpty((string)value))
                    _properties.LoadXml("<Properties/>");
                else
                    _properties.LoadXml((string)value);

                ViewState["ARFProperties"] = _properties.OuterXml;
            }
        }

        public string GetStringProperty(string name)
        {
            EnsureProperties();

            XmlElement oProperty = (XmlElement)_properties.DocumentElement.SelectSingleNode("Property[@Name='" + name + "']");

            if (oProperty == null) return null;

            return oProperty.InnerText;
        }

        public void SetProperty(string name, object value)
        {
            EnsureProperties();

            XmlElement oProperty = (XmlElement)_properties.DocumentElement.SelectSingleNode("Property[@Name='" + name + "']");

            if (oProperty == null)
            {
                oProperty = _properties.CreateElement("Property");
                oProperty.SetAttribute("Name", name);
                _properties.DocumentElement.AppendChild(oProperty);
            }

            oProperty.InnerText = ((value == null) ? "" : value.ToString());

            Value = _properties.OuterXml;
            UpdateFieldValueInItem();
        }

        public bool GetBoolProperty(string name)
        {
            return GetBoolProperty(name, false);
        }

        public bool GetBoolProperty(string name, bool defaultValue)
        {
            EnsureProperties();

            XmlElement oProperty = (XmlElement)_properties.DocumentElement.SelectSingleNode("Property[@Name='" + name + "']");

            if (oProperty == null) return defaultValue;

            bool bResult = defaultValue;

            bool.TryParse(oProperty.InnerText, out bResult);

            return bResult;

        }

        protected override void Render(System.Web.UI.HtmlTextWriter output)
        {
            //base.Render(output);
        }

        protected override void OnLoad(EventArgs e)
        {
            _previous = PreviousControlMode;
            base.OnLoad(e);
        }

        protected override void OnInit(EventArgs e)
        {
            HttpContext.Current.Items[typeof(ARFProperties).ToString()] = this;
            base.OnInit(e);
        }

        public static ARFProperties Current
        {
            get
            {
                try
                {
                    if (HttpContext.Current.Items[typeof(ARFProperties).ToString()] == null)
                        throw new Exception("ARFProperties not found. Please add the <ARF:ARFProperties /> control to your master page after the <form> tag.");

                    return (ARFProperties)HttpContext.Current.Items[typeof(ARFProperties).ToString()];
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex);
                    throw new Exception("Failed to get current ARFProperties control: " + ex.Message, ex);
                }
            }
        }
    }
}
